package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.system.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private UserMapper userMapper;

    // 健康打卡(新增)
    @Override
    public void healthReport(HealthVO healthVO,String username) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        User user = getUser(username);
        // 设置用户id
        health.setUserId(user.getId());
        health.setCreateTime(new Date());
        healthMapper.insert(health);
    }

    // 查询健康打卡
    @Override
    public PageVO<Health> healthHistory(Integer pageNum, Integer pageSize) {
        List<Health> healthList = healthMapper.selectAll();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Health> pageInfo = new PageInfo<>(healthList);
        PageVO<Health> pageVO = new PageVO<>(pageInfo.getTotal(), healthList);
        return pageVO;
    }

    // 查询今日是否打卡
    @Override
    public Boolean healthIsReport(String username) {
        // 获取当前年月日
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get((Calendar.MONTH))+1;
        int day = calendar.get(Calendar.DATE);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(year).append("-").append(month).append("-").append(day);
        System.out.println(stringBuilder.toString());
        // 根据用户名获取用户id
        User user = getUser(username);
        Long userId = user.getId();
        // 查询
        Health health = healthMapper.healthIsReport(userId,stringBuilder.toString());
        if(health!=null){
            return true;
        }else{
            return false;
        }
    }

    // 根据用户名获取用户id
    private User getUser(String username) {
        // 根据用户名获取用户的id
        User user = new User();
        user.setUsername(username);
        user = userMapper.selectOne(user);
        return user;
    }
}
